#include <framework/framework_i.h>

#define INIT_SUBSYSTEM(namestr, initfunc)		\
	 {											\
	 kprintf("%s: ", namestr);					\
	 if ((errno = (initfunc)) < 0) goto error;	\
	 else kprintf("OK.\n");						\
	 }
	 
void
ekf_main(void)
{
	 /* clear the data in the bss segment */
	 memset(_bss, 0, _end - _bss);

	 io_init();
	 io_console_enabled = 1;

	 kprintf("============================================================\n");
	 kprintf("%s %s initailizing\n", __NAME, __VERSION);
	 kprintf("============================================================\n");

	 const char *subsys;
	 int errno;

	 INIT_SUBSYSTEM("SYSCONF", sys_init());

	 if (ekf_sysconf.has_pic)
		  INIT_SUBSYSTEM("PIC", pic_init());

	 if (ekf_sysconf.use_lapic)
		  INIT_SUBSYSTEM("LAPIC", lapic_init());

	 if (ekf_sysconf.ioapic_count > 0)
		  INIT_SUBSYSTEM("IOAPIC", ioapic_init());

	 INIT_SUBSYSTEM("PMEM", pmem_init());
	 INIT_SUBSYSTEM("VMEM", vmem_init());
	 INIT_SUBSYSTEM("INTR", intr_init());
	 INIT_SUBSYSTEM("TASK", task_init());
	 INIT_SUBSYSTEM("CONTEXT", context_init());
	 INIT_SUBSYSTEM("VMEM FIX", vmem_post_fix());
	 INIT_SUBSYSTEM("SYSCONF FIX", sys_post_fix());
	 INIT_SUBSYSTEM("ELF MOD", elf_mod_init());
	 INIT_SUBSYSTEM("LCPU", lcpu_init());
	 
	 kprintf("LCPU %d Start!\n", lcpu_id);
	 lcpu_static[lcpu_id].entry();
	 kprintf("LCPU %d Stop!\n", lcpu_id);
	 while (1) ;

error:
	 kprintf("error while initializing %s subsystem: %d\n", subsys, errno);
	 readline("System shutdown, press enter to reboot\n");
	 reboot();
}

